<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use common\collection\Common;
use common\collection\BackendCommon;
use common\collection\Uploader;
use common\helpers\PublicFun;
use backend\config\routeParams;


/**
 * Drainage controller
 */
class DrainageController extends Controller
{

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public  function beforeAction($action){
        if(BackendCommon::init()){
            if(BackendCommon::rbac()){
                return true;
            }else{
                Yii::$app->response->redirect( ['site/error']);
            }
        }else{
            Yii::$app->response->redirect( ['login/index']);
        }
    }
    //首页
    public function actionIndex()
    {
        return $this->render('index');
    }
    // 砍价活动 
    public function actionBargain()
    {
        return $this->render('bargain');
    }
    // 砍价活动管理
    public function actionBargainactive()
    {
        return $this->render('bargainactive');
    }
    //活动跟踪
    public function actionTracking()
    {
        return $this->render('tracking');
    }
    
    // 新人红包活动管理
    public function actionNewregister()
    {
        return $this->render('newregister');
    }
    // 新人红包活动创建
    public function actionNewregistercreate()
    {
        return $this->render('newregistercreate');
    }
    // 新人活动跟踪 
    public function actionNewregistertracking()
    {
        return $this->render('newregistertracking');
    }

    // 拆礼盒活动
    public function actionOpenbox()
    {
        return $this->render('openbox');
    }
    // 拆礼盒活动创建
    public function actionOpenboxcreate()
    {
        return $this->render('openboxcreate');
    }
    // 拆礼盒活动跟踪
    public function actionOpenboxtracking()
    {
        return $this->render('openboxtracking');
    }
    
    // 活动奖品列表
    public function actionOpenboxgiftlist()
    {
        return $this->render('openboxgiftlist');
    }

    // 大转盘创建
    public function actionTurnplatecreate()
    {
        return $this->render('turnplatecreate');
    }
    // 大转盘列表
    public function actionTurnplate()
    {
        return $this->render('turnplate');
    }
    // 大转盘活动跟踪
    public function actionTurnplatetracking()
    {
        return $this->render('turnplatetracking');
    }
    // 大转盘活动礼品列表
    public function actionTurnplategiftlist()
    {
        return $this->render('turnplategiftlist');
    }
    // 个人领取奖品记录
    public function actionTurnplaterecord()
    {
        return $this->render('turnplaterecord');
    }
    
    // 团购活动列表
    public function actionGroup()
    {
        return $this->render('group');
    }
    // 团购活动跟踪纪录 grouptracking
    public function actionGrouptracking()
    {
        return $this->render('grouptracking');
    }

     // 团购列表
    public function actionGroupList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'PCGameGroupBuyGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }   
    // 团购活动跟踪列表
    public function actionGroupJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'GroupBuyRecordGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    
    
    
    
    

    // 创建砍价活动
    public function actionCreatebargin()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('PCBargainInfoCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
        // print_r($result);exit;
       die(json_encode($result));
    }
    // 砍价活动列表管理
    public function actionDrainageList()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $route = 'PCBargainInfoGetList';
       $url = routeParams::HttpUrl($route);
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       // print_r($result);exit;
       $ret = [
        'PageNo' => $datapost['PageNo'], 
        'PageSize' => $datapost["PageSize"],
        'total' => $result["TotalCounts"],
        'data' => $result['Models']
       ];
       die(json_encode($ret));
    }
    // 删除砍价活动
    public function actionBargainDel()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCBargainInfoDelete');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }
    // 获取砍价活动详情
    public function actionBargainDetial()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCBargainInfoGet');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }
    // 砍价活动列表管理
    public function actionBargainJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'PCBargainRecordGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    // 创建新人红包活动
    public function actionCreatenewregester()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('PCGameGiftPackCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
        // print_r($result);exit;
       die(json_encode($result));
    }

    // 新人红包活动列表管理
    public function actionNewregisterList()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $route = 'PCGameGiftPackGetList';
       $url = routeParams::HttpUrl($route);
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       // print_r($result);exit;
       $ret = [
        'PageNo' => $datapost['PageNo'], 
        'PageSize' => $datapost["PageSize"],
        'total' => $result["TotalCounts"],
        'data' => $result['Models']
       ];
       die(json_encode($ret));
    }
    // 新人红包活动详情
    public function actionNewregisterDetial()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCGameGiftPackGet');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }
    // 新人红包活动删除
    public function actionNewregisterDel()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCGameGiftPackDelete');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

    // 新人红包活动跟踪列表
    public function actionNewregesterJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'PackRecordGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    // 创建拆礼盒活动
    public function actionCreateopenbox()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('PCGameOpenBoxCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
        // print_r($result);exit;
       die(json_encode($result));
    }

    // 拆礼盒活动列表
    public function actionOpenboxList()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $route = 'PCGameOpenBoxGetList';
       $url = routeParams::HttpUrl($route);
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       // print_r($result);exit;
       $ret = [
        'PageNo' => $datapost['PageNo'], 
        'PageSize' => $datapost["PageSize"],
        'total' => $result["TotalCounts"],
        'data' => $result['Models']
       ];
       die(json_encode($ret));
    }
    // 拆礼盒活动详情
    public function actionOpenboxDetial()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCGameOpenBoxGet');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }
    // 拆礼盒活动删除
    public function actionOpenboxDel()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       // $datapost = Yii::$app->request->post();
       // $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCGameOpenBoxDelete');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }
    // 拆礼盒活动跟踪列表
    public function actionOpenboxJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'OpenBoxRecordGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }
    // 拆礼盒活动奖品列表
    public function actionOpenboxgiftgetList()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $route = 'OpenBoxPrizeGetList';
       $url = routeParams::HttpUrl($route);
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       // print_r($result);exit;
       if($datapost['type'] == 100){
          // 正常列表请求
          $ret = [
            'PageNo' => $datapost['PageNo'], 
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
           ];
       }else if($datapost['type'] == 200){
          // 编辑的时候拿到列表 
          $ret = [
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
           ];
       }
       
       die(json_encode($ret));
    }
    // 拆礼盒活动商品创建
    public function actionOpenboxgiftcreate()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('OpenBoxPrizeCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

    // 大转盘活动创建
    public function actionCreateturnplate()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('LuckyWheellCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

    // 大转盘活动列表
    public function actionTurnplateList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'LuckyWheellGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    // 删除大转盘活动
    public function actionTurnplateDel()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('LuckyWheellDelete');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

     // 大转盘活动跟踪列表
    public function actionTurnplateJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'LuckyRecordGetList';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        // print_r($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    // 大转盘活动奖品列表
    public function actionTurnplategiftgetList()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $route = 'WheellPrizeGetList';
       $url = routeParams::HttpUrl($route);
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       // print_r($result);exit;
       if($datapost['type'] == 100){
          // 正常列表请求
          $ret = [
            'PageNo' => $datapost['PageNo'], 
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
           ];
       }else if($datapost['type'] == 200){
          // 编辑的时候拿到列表 
          $ret = [
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
           ];
       }
       die(json_encode($ret));
    }

    // 大转盘活动商品创建
    public function actionTurnplategiftcreate()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $cookies = Yii::$app->request->cookies;
       $admin = $cookies->get('MytipIdBack')->value;
       $datapost['MytipId'] = $admin;
       $url = routeParams::HttpUrl('WheellPrizeCreate');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

    // 拆礼盒活动详情
    public function actionTurnplateDetial()
    {
       $datapost = Yii::$app->request->post();
       $datapost = Common::ArrayFilter($datapost);
       $url = routeParams::HttpUrl('PCGameLuckyWheellGet');
       $result = PublicFun::PostPackage($datapost, $url);
       $result = json_decode($result['Body'],true);
       die(json_encode($result));
    }

     // 大转盘活动个人领取奖品列表
    public function actionTurnplaterecordJoinList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $route = 'WheellReceiveRecord';
        $url = routeParams::HttpUrl($route);
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }
    

    /*
     *  团购活动
     */

    // 创建
    public function actionGroupcreate()
    {
        return $this->render('groupcreate');
    }

    // 大转盘活动商品创建
    public function actionGroupBuyCreate()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $cookies = Yii::$app->request->cookies;
        $admin = $cookies->get('MytipIdBack')->value;
        $datapost['MytipId'] = $admin;
        $url = routeParams::HttpUrl('PCGameGroupBuyCreate');
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }
    //活动详情
    public function actionGroupDetial()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $url = routeParams::HttpUrl('PCGameGroupBuyGet');
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        $result['Model']['ProductDetial'] = stripslashes($result['Model']['ProductDetial']);
        die(json_encode($result));
    }


    // 上传图片
    public function actionEditorUpload()
    {
        header("Content-Type:text/html;charset=utf-8");
        error_reporting( E_ERROR | E_WARNING );
        date_default_timezone_set("Asia/chongqing");

        //上传配置
        $config = array(
            "savePath" => "upload/" ,             //存储文件夹
            "maxSize" => 1000 ,                   //允许的文件最大尺寸，单位KB
            "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
        );
        //上传文件目录
        $Path = "upload/";

        //背景保存在临时目录中
        $config[ "savePath" ] = $Path;
        $up = new Uploader( "upfile" , $config );
        $type = $_REQUEST['type'];
        $callback=$_GET['callback'];
        $info = $up->getFileInfo();

        $fileext = $info['type'];
        $datalist = base64_encode(file_get_contents(Yii::getAlias("@webroot").'/'.$info['url']));
        switch ($fileext) {
            case '.png':
                $EnumImgSuffix = 1;
                break;
            case '.jpg':
                $EnumImgSuffix = 0;
                break;
            case '.gif':
                $EnumImgSuffix = 4;
                break;
        }
        $Data = array(
            "Files" => $datalist,
            "EnumImgSuffix" => $EnumImgSuffix,
            "EnumPathName" => 9,
        );
        $url = routeParams::HttpUrl('ImageUpload');
        $result = PublicFun::PostPackage($Data, $url);
        $result = json_decode($result['Body'],true);
        $Photo =routeParams::HttpUrl('IMGURL') . "/" . $result['FileName'];
        $info['url'] = $Photo;
        /**
         * 返回数据
         */
        if($callback) {
            echo '<script>'.$callback.'('.json_encode($info).')</script>';
        } else {
            echo json_encode($info);
        }




    }

    
   
  

}
